package com.qtjf.qtmoneyservice.fragment;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.activity.BaseActivity;
import com.qtjf.qtmoneyservice.activity.BaseInfoActivity;
import com.qtjf.qtmoneyservice.activity.BiBeiRZActivity;
import com.qtjf.qtmoneyservice.activity.FirstRenZhengActivity;
import com.qtjf.qtmoneyservice.activity.JdRenZhengActivity;
import com.qtjf.qtmoneyservice.activity.KaRenZhengActivity;
import com.qtjf.qtmoneyservice.activity.LoginActivity;
import com.qtjf.qtmoneyservice.activity.MyselfInfoActivity;
import com.qtjf.qtmoneyservice.activity.PhoneRenZhengActivity;
import com.qtjf.qtmoneyservice.activity.ShenFenActivity;
import com.qtjf.qtmoneyservice.activity.XueXinActivity;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import java.util.ArrayList;
import java.util.List;


public class ReZhengFragment extends Fragment {

    LinearLayout layout1;
    LinearLayout layout2;
    LinearLayout layout3;
    LinearLayout layout4;
    LinearLayout layout5;
    LinearLayout layout6;

    LinearLayout layout8;
    LinearLayout layout9;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_renzheng, container, false);

        initView(view);
        initData();
        initEvent();

        MyApplication.getApi().handleIntent(getActivity().getIntent(), new IWXAPIEventHandler() {
            @Override
            public void onReq(BaseReq baseReq) {
                Toast.makeText(getContext(),baseReq.toString(),Toast.LENGTH_LONG).show();
                Log.d("xxx1",baseReq.toString());
            }

            @Override
            public void onResp(BaseResp baseResp) {
                Toast.makeText(getContext(),baseResp.toString(),Toast.LENGTH_LONG).show();
                Log.d("xxx2",baseResp.toString());
            }
        });

        return view;
    }

    private void initView(View view) {
        layout1= (LinearLayout) view.findViewById(R.id.layout1);
        layout2= (LinearLayout) view.findViewById(R.id.layout2);
        layout3= (LinearLayout) view.findViewById(R.id.layout3);
        layout4= (LinearLayout) view.findViewById(R.id.layout4);
        layout5= (LinearLayout) view.findViewById(R.id.layout5);
        layout6= (LinearLayout) view.findViewById(R.id.layout6);
        layout8= (LinearLayout) view.findViewById(R.id.layout8);
        layout9= (LinearLayout) view.findViewById(R.id.layout9);

    }

    private void initData() {

    }

    private void initEvent() {
        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    startActivity(new Intent(getContext(), MyselfInfoActivity.class));
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });

        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    startActivity(new Intent(getContext(), PhoneRenZhengActivity.class));
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });

        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    startActivity(new Intent(getContext(), KaRenZhengActivity.class));
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });

        layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    startActivity(new Intent(getContext(), ShenFenActivity.class));
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });

        layout5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wechat_sdk_demo_test";
                    MyApplication.getApi().sendReq(req);
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });

        layout6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    TestContact();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        layout8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    startActivity(new Intent(getContext(), XueXinActivity.class));
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });

        layout9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constant.getShiMingState().equals("1")) {
                    startActivity(new Intent(getContext(), JdRenZhengActivity.class));
                }else
                {
                    startActivity(new Intent(getContext(), FirstRenZhengActivity.class));
                }
            }
        });
    }

    //查询所有联系人的姓名，电话，邮箱
    public void TestContact() throws Exception {
        Uri uri = Uri.parse("content://com.android.contacts/contacts");
        ContentResolver resolver = getContext().getContentResolver();

        Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);

        List<String> lists=new ArrayList<String>();
        while (cursor.moveToNext()) {

            int contractID = cursor.getInt(0);
            StringBuilder sb = new StringBuilder("contractID=");
            sb.append(contractID);
            uri = Uri.parse("content://com.android.contacts/contacts/" + contractID + "/data");
            Cursor cursor1 = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null);
            while (cursor1.moveToNext()) {
                String data1 = cursor1.getString(cursor1.getColumnIndex("data1"));
                String mimeType = cursor1.getString(cursor1.getColumnIndex("mimetype"));
                if ("vnd.android.cursor.item/name".equals(mimeType)) { //是姓名
                    sb.append(",name=" + data1);
                } else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { //邮箱
                    sb.append(",email=" + data1);
                } else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { //手机
                    sb.append(",phone=" + data1);
                }
            }
            cursor1.close();
            lists.add(sb.toString());
        }
        Toast.makeText(getContext(),lists.get(0)+lists.get(1),Toast.LENGTH_SHORT).show();
        cursor.close();
    }

}
